﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace Loong.Admin.AuditLogging
{
    public class EntityChangeConfiguration : IEntityTypeConfiguration<EntityChange>
    {
        public void Configure(EntityTypeBuilder<EntityChange> b)
        {
            b.ToTable("EntityChange");

            b.ConfigureByConvention();

            b.Property(x => x.EntityTypeFullName).HasMaxLength(EntityChangeConsts.MaxEntityTypeFullNameLength).IsRequired().HasColumnName(nameof(EntityChange.EntityTypeFullName));
            b.Property(x => x.EntityId).HasMaxLength(EntityChangeConsts.MaxEntityIdLength).HasColumnName(nameof(EntityChange.EntityId));
            b.Property(x => x.AuditLogId).IsRequired().HasColumnName(nameof(EntityChange.AuditLogId));
            b.Property(x => x.ChangeTime).IsRequired().HasColumnName(nameof(EntityChange.ChangeTime));
            b.Property(x => x.ChangeType).IsRequired().HasColumnName(nameof(EntityChange.ChangeType));
            b.Property(x => x.TenantId).HasColumnName(nameof(EntityChange.TenantId));

            b.HasMany(a => a.PropertyChanges).WithOne().HasForeignKey(x => x.EntityChangeId);

            b.HasIndex(x => new { x.AuditLogId });
            b.HasIndex(x => new { x.TenantId, x.EntityTypeFullName, x.EntityId });

            b.ApplyObjectExtensionMappings();
        }
    }
}
